<template>
  <view class="container">
    <view class="background-wrapper">
      <image class="background" src="/static/login.jpg" mode="aspectFill"></image>
      <view class="overlay"></view>
    </view>
    
    <view class="content">
      <view class="forgot-card">
        <view class="header">
          <text class="title">找回密码</text>
          <text class="subtitle">请填写账号信息</text>
        </view>
        
        <view class="form">
          <view class="input-group">
            <text class="label">账号：</text>
            <input 
              type="text" 
              placeholder="请输入账号" 
              v-model="username" 
              class="input-field"
              placeholder-class="placeholder" 
            />
          </view>
          
          <view class="input-group">
            <text class="label">手机号：</text>
            <input 
              type="number" 
              placeholder="请输入手机号" 
              v-model="phone" 
              class="input-field"
              placeholder-class="placeholder" 
            />
          </view>
          
          <view class="input-group code-group">
            <text class="label">验证码：</text>
            <input 
              type="number" 
              placeholder="请输入验证码" 
              v-model="verifyCode" 
              class="input-field"
              placeholder-class="placeholder" 
            />
            <view class="code-btn" @tap="getVerifyCode">
              <text>{{ codeText }}</text>
            </view>
          </view>
          
          <view class="input-group">
            <text class="label">新密码：</text>
            <input 
              type="password" 
              placeholder="请设置新密码" 
              v-model="newPassword" 
              class="input-field"
              placeholder-class="placeholder" 
            />
          </view>
          
          <view class="input-group">
            <text class="label">确认密码：</text>
            <input 
              type="password" 
              placeholder="请确认新密码" 
              v-model="confirmPassword" 
              class="input-field"
              placeholder-class="placeholder" 
            />
          </view>
          
          <button @tap="resetPassword" class="reset-btn" hover-class="button-hover">
            <text>重置密码</text>
          </button>
        </view>
        
        <view class="links">
          <text @tap="goLogin" class="link-text">返回登录</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      username: '',
      phone: '',
      verifyCode: '',
      newPassword: '',
      confirmPassword: '',
      countDown: 60,
      counting: false,
      timer: null,
      codeText: '获取验证码',
      loading: false
    }
  },
  methods: {
    getVerifyCode() {
      if (this.counting) return;
      
      if (!this.phone || !/^1\d{10}$/.test(this.phone)) {
        uni.showToast({
          title: '请输入正确的手机号',
          icon: 'none'
        });
        return;
      }
      
      // 调用获取验证码接口
      uni.request({
        url: 'https://m1.apifoxmock.com/m1/6102433-5793108-default/admin/verification-code',
        method: 'POST',
        data: {
          phone: this.phone
        },
        header: {
          'Content-Type': 'application/json'
        },
        success: (res) => {
          console.log('获取验证码结果:', res);
          if (res.statusCode === 200 || res.data.code === 200) {
            uni.showToast({
              title: '验证码已发送',
              icon: 'none'
            });
            
            // 倒计时
            this.startCountdown();
          } else {
            uni.showToast({
              title: res.data && res.data.msg ? res.data.msg : '获取验证码失败',
              icon: 'none'
            });
          }
        },
        fail: (err) => {
          console.error('获取验证码失败:', err);
          uni.showToast({
            title: '获取验证码失败',
            icon: 'none'
          });
        }
      });
    },
    
    startCountdown() {
      this.counting = true;
      this.countDown = 60;
      this.codeText = `${this.countDown}s`;
      
      this.timer = setInterval(() => {
        this.countDown--;
        this.codeText = `${this.countDown}s`;
        
        if (this.countDown <= 0) {
          clearInterval(this.timer);
          this.counting = false;
          this.codeText = '获取验证码';
        }
      }, 1000);
    },
    
    validateForm() {
      if (!this.username.trim()) {
        uni.showToast({
          title: '请输入账号',
          icon: 'none'
        });
        return false;
      }
      
      if (!this.phone || !/^1\d{10}$/.test(this.phone)) {
        uni.showToast({
          title: '请输入正确的手机号',
          icon: 'none'
        });
        return false;
      }
      
      if (!this.verifyCode) {
        uni.showToast({
          title: '请输入验证码',
          icon: 'none'
        });
        return false;
      }
      
      if (!this.newPassword) {
        uni.showToast({
          title: '请设置新密码',
          icon: 'none'
        });
        return false;
      }
      
      if (this.newPassword.length < 6) {
        uni.showToast({
          title: '密码长度不能少于6位',
          icon: 'none'
        });
        return false;
      }
      
      if (this.newPassword !== this.confirmPassword) {
        uni.showToast({
          title: '两次密码输入不一致',
          icon: 'none'
        });
        return false;
      }
      
      return true;
    },
    
    resetPassword() {
      if (!this.validateForm() || this.loading) {
        return;
      }
      
      this.loading = true;
      uni.showLoading({
        title: '提交中...'
      });
      
      // 调用重置密码接口
      uni.request({
        url: 'https://m1.apifoxmock.com/m1/6102433-5793108-default/admin/updatePassword',
        method: 'PUT',
        data: {
          username: this.username,
          phone: this.phone,
          password: this.newPassword,
          jwt: this.verifyCode
        },
        header: {
          'Content-Type': 'application/json'
        },
        success: (res) => {
          console.log('重置密码结果:', res);
          
          if (res.statusCode === 200 && res.data) {
            if (res.data.code === 0 || res.data.code === 200) {
              // 重置成功
              uni.showToast({
                title: '密码重置成功',
                icon: 'success',
                duration: 2000,
                success: () => {
                  setTimeout(() => {
                    uni.navigateBack();
                  }, 2000);
                }
              });
            } else {
              // 重置失败
              uni.showToast({
                title: res.data.msg || '重置密码失败',
                icon: 'none'
              });
            }
          } else {
            uni.showToast({
              title: '重置密码失败',
              icon: 'none'
            });
          }
        },
        fail: (err) => {
          console.error('重置密码请求失败:', err);
          uni.showToast({
            title: '重置密码失败，请检查网络',
            icon: 'none'
          });
        },
        complete: () => {
          this.loading = false;
          uni.hideLoading();
        }
      });
    },
    
    goLogin() {
      uni.navigateBack();
    }
  },
  onUnload() {
    if (this.timer) {
      clearInterval(this.timer);
    }
  }
}
</script>

<style lang="scss">
.container {
  position: relative;
  height: 100vh;
  width: 100%;
}

.background-wrapper {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

.background {
  width: 100%;
  height: 100%;
}

.overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(
    to bottom,
    rgba(0, 0, 0, 0.4),
    rgba(0, 0, 0, 0.7)
  );
}

.content {
  position: relative;
  z-index: 1;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 20px;
}

.forgot-card {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 20px;
  padding: 40px 30px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
  animation: fadeIn 0.5s ease-out;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.header {
  text-align: center;
  margin-bottom: 40px;
}

.title {
  font-size: 32px;
  font-weight: 600;
  color: #333;
  margin-bottom: 10px;
}

.subtitle {
  font-size: 16px;
  color: #666;
}

.form {
  margin-bottom: 30px;
}

.input-group {
  background: #f5f5f5;
  border-radius: 12px;
  margin-bottom: 20px;
  display: flex;
  align-items: center;
  padding: 4px 16px;
  transition: all 0.3s ease;
  
  &:focus-within {
    background: #fff;
    box-shadow: 0 0 0 2px rgba(255, 81, 81, 0.2);
  }
  
  &.code-group {
    padding-right: 8px;
  }
}

.label {
  font-size: 16px;
  color: #666;
  margin-right: 12px;
  white-space: nowrap;
}

.input-field {
  flex: 1;
  height: 52px;
  font-size: 16px;
  background: transparent;
}

.placeholder {
  color: #999;
}

.code-btn {
  background: linear-gradient(135deg, #ff5151, #ff8383);
  border-radius: 8px;
  padding: 0 16px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  
  text {
    color: #fff;
    font-size: 14px;
    white-space: nowrap;
  }
}

.reset-btn {
  background: linear-gradient(135deg, #ff5151, #ff8383);
  height: 52px;
  border-radius: 12px;
  font-size: 18px;
  color: #fff;
  font-weight: 600;
  margin-top: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
  border: none;
  letter-spacing: 1px;
}

.button-hover {
  transform: translateY(2px);
  box-shadow: 0 5px 15px rgba(255, 81, 81, 0.3);
  opacity: 0.9;
}

.links {
  display: flex;
  justify-content: center;
  margin-top: 25px;
}

.link-text {
  color: #ff5151;
  font-size: 14px;
  position: relative;
  padding: 5px;
  
  &:after {
    content: '';
    position: absolute;
    bottom: -2px;
    left: 0;
    width: 100%;
    height: 1px;
    background: #ff5151;
    transform: scaleX(0);
    transition: transform 0.3s ease;
  }
  
  &:hover:after {
    transform: scaleX(1);
  }
}
</style> 

